package com.eastfair.venuebooking.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.dao.ExhibitionSegmentMapper;
import com.eastfair.venuebooking.dto.ExhibitionSegmentDTO;
import com.eastfair.venuebooking.entity.ExhibitionSegment;
import com.eastfair.venuebooking.entity.ExhibitionTask;
import com.eastfair.venuebooking.enums.SegmentNameEnum;
import com.eastfair.venuebooking.service.ExhibitionSegmentService;
import com.eastfair.venuebooking.service.ExhibitionTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 环节
 * </p>
 *
 * @author linan
 * @date 2022-06-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionSegmentServiceImpl extends SuperServiceImpl<ExhibitionSegmentMapper, ExhibitionSegment> implements ExhibitionSegmentService {

    @Resource
    private ExhibitionTaskService exhibitionTaskService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionSegment> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }


    @Override
    protected R<Boolean> handlerSave(ExhibitionSegment model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        //雪花ID
        return R.successDef();
    }

    @Override
    public R addExhibitionSegment(ExhibitionSegmentDTO exhibitionSegmentSaveDTO) {
        ExhibitionSegment exhibitionSegment = new ExhibitionSegment();
        exhibitionSegment.setSegmentName(exhibitionSegmentSaveDTO.getSegmentName());
        exhibitionSegment.setExhibitionId(exhibitionSegmentSaveDTO.getExhibitionId());
        exhibitionSegment.setStartTime(exhibitionSegmentSaveDTO.getStartTime());
        exhibitionSegment.setEndTime(exhibitionSegmentSaveDTO.getEndTime());
        save(exhibitionSegment);
        return R.success(exhibitionSegment.getId());
    }

    @Override
    public void updateDeleteAble(Long segmentId, Boolean deleteAble) {
        if(Objects.nonNull(segmentId)){
            ExhibitionSegment updateEntity = new ExhibitionSegment();
            updateEntity.setId(segmentId);
            updateEntity.setDeleteable(deleteAble);
            this.updateById(updateEntity);
        }
    }

    @Override
    public void updateDeleteAble(ExhibitionTask exhibitionTask) {
        if(Objects.nonNull(exhibitionTask) && Objects.nonNull(exhibitionTask.getSegmentId())){
            // 通过展会任务修改没有绑定任务的环节可删除状态为"可以删除"
            QueryWrapper<ExhibitionTask> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ExhibitionTask::getSegmentId,exhibitionTask.getSegmentId())
                    .eq(ExhibitionTask::getIsDeleted,BusinessConstant.NO)
                    .notIn(ExhibitionTask::getSegmentName, Arrays.stream(SegmentNameEnum.values()).map(SegmentNameEnum::getDesc).collect(Collectors.toList()));
            List<ExhibitionTask> list = exhibitionTaskService.list(queryWrapper);
            // 如果环节下没有任务，且不是默认三个环节，则修改可删除状态为"可以删除"
            if(CollectionUtils.isEmpty(list)){
                updateDeleteAble(exhibitionTask.getSegmentId(),true);
            }
        }
    }
}
